package com.deanlib.cabinet.service;

import android.app.PendingIntent;
import android.content.Intent;

import com.deanlib.cabinet.R;
import com.deanlib.cabinet.app.Constants;
import com.deanlib.cabinet.base.BaseService;
import com.deanlib.cabinet.data.AppDatabase;
import com.deanlib.cabinet.data.db.ResourceDao;
import com.deanlib.cabinet.data.event.ConfigChangeEvent;
import com.deanlib.cabinet.data.event.ResourceChangeEvent;
import com.deanlib.cabinet.data.event.UserStateEvent;
import com.deanlib.cabinet.data.model.Resource;
import com.deanlib.cabinet.ui.view.MainActivity;
import com.deanlib.cabinet.utils.ConfigManager;
import com.deanlib.cabinet.utils.OssManager;
import com.deanlib.cabinet.utils.UserManager;
import com.elvishew.xlog.XLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 资源同步服务
 * @Created by Dean
 * @Date 2023/6/7
 */
public class ResourceSyncService extends BaseService {

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventUserState(UserStateEvent event) {
        if (UserManager.getInstance().isLogin()) {
            syncLocalResource();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventResourceChange(ResourceChangeEvent event) {
        if (UserManager.getInstance().isLogin()) {
            syncLocalResource();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void eventConfigChange(ConfigChangeEvent event) {
        if (UserManager.getInstance().isLogin()) {
            syncLocalResource();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        syncLocalResource();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getNotifyId() {
        return Constants.SERVICE_NOTIFY_ID_RESOURCE_SYNC;
    }

    @Override
    public String getNotifyContent() {
        return getString(R.string.synchronizing_data);
    }

    @Override
    public PendingIntent getNotifyPendingIntent() {
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, -2
                , new Intent(this, MainActivity.class)
                , android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ?
                        PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }

    /**
     * 把本地资源同步到服务器
     */
    private void syncLocalResource() {
        if (UserManager.getInstance().isLogin() && ConfigManager.getInstance().enableSync()) { //todo 当打开同步开关的时候也需要如下操作
            //查询数据库
            List<Resource> list = AppDatabase.getDaoSession().getResourceDao().queryBuilder()
                    .whereOr(ResourceDao.Properties.Synced.isNull(), ResourceDao.Properties.Synced.eq(false))
                    .whereOr(ResourceDao.Properties.NotAllowShared.isNull(), ResourceDao.Properties.NotAllowShared.notEq(true))
                    .list();
            XLog.i("syncLocalResource list size: " + list.size());
            if (!list.isEmpty()) {
                OssManager.getInstance().addTasks(list.stream().map(OssManager.Task::new).collect(Collectors.toList()));
            }
        }
    }
}
